using UnityEngine;
using System.Collections;


[RequireComponent(typeof(ItemSelector))]
public class MenuHandler : MonoBehaviour {

    // ----- configurable via Unity --------------------------------------------
    public Transform[] objects;
    public Transform[,] items;
    public Fader xFader;
    public Fader yFader;


    // ----- internal variables -----------------------------------------------
    ItemSelector selector;
    int[] selectionCoordinates;
    int[] lastSelectionCoordinates;


    // ----- internal helpers -----------------------------------------------
    int x {
        get { return selectionCoordinates[0]; }
    }

    int y {
        get { return selectionCoordinates[1]; }
    }


    // ----- internal methods --------------------------------------------------
    void Start() {
        if (!xFader || !yFader) {
            Debug.LogError("Please add faders to " + gameObject.name);
            return;
        }

        items = new Transform[4,4];
        items[0,0] = objects[0];
        items[0,1] = objects[1];
        items[0,2] = objects[2];
        items[1,0] = objects[3];
        items[1,1] = objects[4];
        items[1,2] = objects[5];
        items[2,0] = objects[6];
        items[2,1] = objects[7];
        items[2,2] = objects[8];

        selector = gameObject.AddComponent<ItemSelector>();
        selector.numItemsX = items.GetLength(0)-1;
        selector.numItemsY = items.GetLength(1)-1;
        selector.xFader = xFader;
        selector.yFader = yFader;

        resetSelection();
        lastSelectionCoordinates = new int[2] {1, 1};

        if (GetComponent<PushDetector>() == null) {
            gameObject.AddComponent<PushDetector>();
        }
    }

    void Hand_Create(Vector3 pos){
        HighlightItem(lastSelectionCoordinates);
    }

    void Hand_Destroy() {
        Unhighlight();
    }

    void PushDetector_Push() {
        SendMessage("Menu_Select", items[x,y], SendMessageOptions.DontRequireReceiver);
    }

    void PushDetector_Release() {
        if (x == -1 || y == -1) return;
        SendMessage("Menu_Highlight", items[x,y], SendMessageOptions.DontRequireReceiver);
    }

    // ----- custom callbacks -------------------------------------------------
    void ItemSelector_Select(int[] itemIndex) {
        if (itemIndex != selectionCoordinates) {
            Unhighlight();
            HighlightItem(itemIndex);
        }
    }


    // ----- private methods --------------------------------------------------
    private void HighlightItem(int[] itemCoordinates)  {
        selectionCoordinates = itemCoordinates;
        SendMessage("Menu_Highlight", items[x,y], SendMessageOptions.DontRequireReceiver);
    }

    private void Unhighlight() {
        if (x == -1 || y == -1) return;
        SendMessage("Menu_Unhighlight", items[x,y], SendMessageOptions.DontRequireReceiver);
        lastSelectionCoordinates = selectionCoordinates;
        resetSelection();
    }

    private void resetSelection() {
        selectionCoordinates = new int[2] {-1, -1};
    }
}
